<template>
  <el-main>
    <el-button class="list-btn" type="primary" icon="el-icon-plus" @click="createItem" v-if="btnShow">创建</el-button>
    <el-table :data="tableData" :row-class-name="tableRowClassName" v-if="listShow">
      <el-table-column prop="num" label="序号"></el-table-column>
      <el-table-column prop="chinesename" label="中文名"></el-table-column>
      <el-table-column prop="foreignname" label="英文名"></el-table-column>
      <el-table-column prop="alias" label="别名"></el-table-column>
      <el-table-column prop="introduction" label="介绍"></el-table-column>
      <el-table-column prop="nationality" label="国籍"></el-table-column>
      <el-table-column prop="constellation" label="星座"></el-table-column>
      <el-table-column prop="sex" label="性别"></el-table-column>
      <el-table-column prop="birthday" label="生日"></el-table-column>
      <el-table-column prop="heat" label="热度"></el-table-column>
      <el-table-column prop="id" label="操作">
        <template slot-scope="scope">
          <el-button
            class="list-btn"
            type="success"
            @click="editItem(scope.$index,scope.row)"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button 
            class="list-btn"
            type="danger"
            @click="deleteItem(scope.$index,scope.row)"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-form ref="form" :model="form" label-width="80px" v-if="editShow">
      <el-form-item label="中文名">
        <el-input v-model="form.chinesename"></el-input>
      </el-form-item>
      
      <el-form-item label="英文名">
        <el-input v-model="form.foreignname"></el-input>
      </el-form-item>
      
      <el-form-item label="别名">
        <el-input v-model="form.alias"></el-input>
      </el-form-item>
      
      <el-form-item label="介绍">
        <el-input v-model="form.introduction"></el-input>
      </el-form-item>
      
      <el-form-item label="国籍">
        <el-input v-model="form.nationality"></el-input>
      </el-form-item>
      
      <el-form-item label="星座">
        <el-input v-model="form.constellation"></el-input>
      </el-form-item>
      
      <el-form-item label="性别">
        <el-input v-model="form.sex"></el-input>
      </el-form-item>
      
       <el-form-item label="热度">
        <el-input v-model="form.heat"></el-input>
      </el-form-item>
      
      <el-form-item label="生日">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" v-model="form.birthday" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
      
      <el-form-item label="照片">
        <el-select v-model="form.picture" value-key="id" placeholder="请选择歌手照片">
        <el-option label="无" :value="null" ></el-option>
          <el-option v-for="picture in edit.pictures" :key="picture.id" :label="picture.resourcePath" :value="picture"></el-option>
        </el-select>
      </el-form-item>
      
      <el-form-item label="专辑">
        <el-checkbox-group v-model="form.albums" v-if="newShow==false">
          <el-checkbox
            v-for="album in edit.albums"
            :key="album.id"
            :label="album"
            :checked="check(edit.singer.albums, album)"
          >{{album.name}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.albums" v-else>
          <el-checkbox
            v-for="album in edit.albums"
            :key="album.id"
            :label="album"
          >{{album.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      
      <el-form-item label="歌曲">
        <el-checkbox-group v-model="form.songs" v-if="newShow==false">
          <el-checkbox
            v-for="song in edit.songs"
            :key="song.id"
            :label="song"
            :checked="check(edit.singer.songs, song)"
            name="type"
          >{{song.name}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.songs" v-else>
          <el-checkbox
            v-for="song in edit.songs"
            :key="song.id"
            :label="song"
            name="type"
          >{{song.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      
      <el-form-item label="分类">
        <el-checkbox-group v-model="form.classifications" v-if="newShow==false">
          <el-checkbox
            v-for="classification in edit.classifications"
            :key="classification.id"
            :label="classification"
            :checked="check(edit.singer.classifications, classification)"
            name="type"
          >{{classification.name}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.classifications" v-else>
          <el-checkbox
            v-for="classification in edit.classifications"
            :key="classification.id"
            :label="classification"
            name="type"
          >{{classification.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
       
       <el-form-item label="关注的用户">
        <el-checkbox-group v-model="form.users" v-if="newShow==false">
          <el-checkbox
            v-for="user in edit.users"
            :key="user.id"
            :label="user"
            :checked="check(edit.singer.users, user)"
            name="type"
          >{{user.username}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.users" v-else>
          <el-checkbox
            v-for="user in edit.users"
            :key="user.id"
            :label="user"
            name="type"
          >{{user.username}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
       
       <el-form-item label="所属组合">
        <el-checkbox-group v-model="form.combinations" v-if="newShow==false">
          <el-checkbox
            v-for="combination in edit.combinations"
            :key="combination.id"
            :label="combination"
            :checked="check(edit.singer.combinations, combination)"
            name="type"
          >{{combination.name}}</el-checkbox>
        </el-checkbox-group>
        <el-checkbox-group v-model="form.combinations" v-else>
          <el-checkbox
            v-for="combination in edit.combinations"
            :key="combination.id"
            :label="combination"
            name="type"
          >{{combination.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
       
      <el-form-item>
        <el-button type="primary" @click="onSubmit" v-if="newShow==false">确定</el-button>
        <el-button type="primary" @click="add" v-else>确定</el-button>
        <el-button @click="back">取消</el-button>
      </el-form-item>
    </el-form>
  


  </el-main>
</template>

<script type="module">
var singers = [];
var formData = [];
var editData = [];
var delFlag = false;
module.exports = {
  data: function() {
    return {
      tableData: singers,
      editShow: false,
      listShow: true,
      newShow: false,
      btnShow: true,
      edit: editData,
      emptyForm: {
        id: "",
        chinesename: "",
        foreignname: "",
        alias: "",
        introduction: "",
        nationality: "",
        constellation: "",
        sex: "",
        birthday: "",
        heat: "",
        albums: [],
        picture: "",
        songs: [],
        classifications: [],
        users: [],
        combinations: [],
      },
      form: {
        id: "",
        chinesename: "",
        foreignname: "",
        alias: "",
        introduction: "",
        nationality: "",
        constellation: "",
        sex: "",
        birthday: "",
        heat: "",
        albums: [],
        picture: "",
        songs: [],
        classifications: [],
        users: [],
        combinations: [],
      }
    };
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    gettable() {
      $.ajax({
        url: "/listAllSingers",
        type: "get",
        dataType: "json",
        async: false,
        success(data) {
          console.log(data.singers);
          for (let i in data.singers) {
            singers[i] = {
              num: i,
              chinesename: data.singers[i].chinesename,
              foreignname: data.singers[i].foreignname,
              alias: data.singers[i].alias,
              introduction: data.singers[i].introduction,
              nationality: data.singers[i].nationality,
              constellation: data.singers[i].constellation,
              sex: data.singers[i].sex,
              birthday: data.singers[i].birthday,
              heat: data.singers[i].heat,
              id: data.singers[i].id
            };
          }
        }
      });
    },
    deleteItem(item, index) {
      let id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/deleteSinger",
        type: "post",
        data: JSON.parse(id),
        async: false,
        success(data) {
          if(data) {
            delFlag = true;
          } else {
            delFlag = false;
          } 
        }
      });
      if(delFlag) {
        this.$message({
          message: '恭喜你，删除成功',
          type: 'success'
        });
        setTimeout(function(){
          window.location.href = "/index?page=9";
        }, 1500);
      } else {
        this.$message.error('错误窝，删除失败');
      }
    },
    editItem(item, index) {
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      let id = '{"id": ' + index.id + "}";
      $.ajax({
        url: "/editSinger",
        type: "get",
        dataType: "json",
        async: false,
        data: JSON.parse(id),
        success(data) {
          console.log(data);
          editData = {
            singer: data.singer,
            albums: data.albums,
            pictures: data.pictures,
            songs: data.songs,
            classifications: data.classifications,
            users: data.users,
            combinations: data.combinations,
          };
          console.log(editData);
          formData = {
            id: data.singer.id,
            chinesename: data.singer.chinesename,
            foreignname: data.singer.foreignname,
            alias: data.singer.alias,
            introduction: data.singer.introduction,
            nationality: data.singer.nationality,
            constellation: data.singer.constellation,
            sex: data.singer.sex,
            birthday: data.singer.birthday,
            heat: data.singer.heat,
            albums: [],
            picture: data.singer.picture,
            songs: [],
            classifications: [],
            users: [],
            combinations: [],
          };
        }
      });
      this.edit = editData;
      this.form = formData;
    },
    check(objs, obj) {
      for (let i in objs) {
        if (objs[i].id == obj.id) return true;
      }
      return false;
    },
    onSubmit() {
      console.log(JSON.stringify(this.form));
      $.ajax({
        url: "/updateSinger",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=9";
        }
      });
    },
    back() {
      this.listShow = true;
      this.editShow = false;
      this.btnShow = true;
      this.emptyForm = {
        id: "",
        chinesename: "",
        foreignname: "",
        alias: "",
        introduction: "",
        nationality: "",
        constellation: "",
        sex: "",
        birthday: "",
        heat: "",
        albums: [],
        picture: "",
        songs: [],
        classifications: [],
        users: [],
        combinations: [],
      };
    },
    createItem() {
      this.newShow = true;
      this.listShow = false;
      this.editShow = true;
      this.btnShow = false;
      $.ajax({
        url: "/newSinger",
        type: "get",
        dataType: "json",
        async: false,
        success(data) {
         editData = {
            albums: data.albums,
            pictures: data.pictures,
            songs: data.songs,
            classifications: data.classifications,
            users: data.users,
            combinations: data.combinations,
          };
 }
      });
      this.edit = editData;
       this.form = this.emptyForm;
    },
    add() {
      console.log(JSON.stringify(this.form));
      $.ajax({
        url: "/addSinger",
        type: "post",
        contentType: "application/json",
        async: false,
        data: JSON.stringify(this.form),
        success(data) {
          window.location.href = "/index?page=9";
        }
      });
    }
  },
  mounted: function() {
    // console.log("3");
    this.gettable();
    // console.log("4");
  }
};
</script>